package com.hykj.xxltest.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.config.Config;
import com.hykj.xxltest.utils.IntegerUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * conditon 加上PageSize 指定分页数
 * conditon 加上noD      不装饰
 */

@Service
public abstract class BaseService<D extends BaseModel, M extends BaseMapper<D>>{
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    protected M mapper;


    /**
     * 判断是不是要创建的model是不是合法 不合法就抛异常
     * @param model
     */
    protected void checkIllegal(D model) throws MsgException {
        return ;
    }

    /**
     * 对返回的Model进行装饰后再返回
     * @param model
     * @param condition
     * @return
     */
    protected D change(D model,Map condition) {
        return model;
    }

    private D beforeChange(D model, Map condition) {
        if (model == null) {
            return null;
        }
        if (condition == null) {
            return model;
        }
        if(condition.get("noD")!=null){ //不装饰
            return model;
        }
        return change(model,condition);
    }



    public PageInfo<D> getDList(Map condition, int page) {
        if(condition.get("pageSize")!=null){
            Integer pageSize= IntegerUtil.objToInt(condition.get("pageSize"));
            PageHelper.startPage(page, pageSize);
        }else {
            PageHelper.startPage(page, Config.PageSize);
        }
        List<D> models = getListLite(condition);
        PageInfo<D> p = new PageInfo<>(models);
        p.setList(changeList(models,condition));
        return p;
    }

    protected    List<D> changeList(List<D> models,Map condition){
        List<D> stream = models.stream().map(model -> beforeChange(model,condition)).collect(Collectors.toList());
        return stream;
    }



    public List<D> getDList(Map condition) {
        List<D> list = getListLite(condition);

        return changeList(list,condition);
    }

    public List<D> getListLite(Map condition) {
        List<D> list = mapper.getList(condition);
        return list;

    }

    public D getById(Integer id) {
        if(id==null){
            return null;
        }
        if(id<=0){
            return null;
        }
        D model = mapper.getById(id);
        return model;
    }

    public D getByIdD(Integer id) {

        return getByIdD(id,new HashMap());
    }

    public D getByIdD(Integer id,Map condition) {
        if(id==null){
            return null;
        }
        D model = getById(id);
        if(model==null){
            return null;
        }
        condition.put("detail",1);
        return beforeChange(model,condition);
    }

    public Integer getCount(Map condition) {
      int count= (int) PageHelper.count(() -> mapper.getList(condition));
      return count;
    }

    public boolean update(D model) {
        checkIllegal(model);
        return mapper.update(model) > 0 ;
    }

    public boolean create(D model) {
        checkIllegal(model);
        return mapper.create(model) > 0;
    }


    //@Deprecated
    /**
     * 批量删除 慎用
     */
    public Integer delete(Map condition) {

        if (condition.size()==0) {
            logger.error("删除条件没加 你差点要删库跑路了啊");
            return 0;
        }
        return mapper.delete(condition);

    }

    public boolean deleteById(Integer id) {
        return mapper.deleteById(id) > 0 ;
    }

    //批量更新
    public void batchUpdate(List<D> list) {
        mapper.batchUpdate(list);
    }

    //统计
    public  Map<String,Object>  getStatistics(Map map) {
        return mapper.getStatistics(map);
    }

    //将结果集 list转map
    public Map<Integer,D> listToMap(List<D> list){
        Map map=new HashMap();
        for (D model:list){
            if (model==null){
                continue;
            }
            map.put(model.getId(),model);
        }
        return map;
    }
}
